package com.kaiyiweitong.tongxing.fragment.after_sell;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.GridLayout;

import com.kaiyiweitong.tongxing.R;
import com.kaiyiweitong.tongxing.activity.my.AfterSellActivity;
import com.kaiyiweitong.tongxing.adapter.AfterSellItemAdapter;
import com.kaiyiweitong.tongxing.bean.AfterSellResData;
import com.kaiyiweitong.tongxing.constant.HttpConstant;
import com.kaiyiweitong.tongxing.constant.LogicConstant;
import com.kaiyiweitong.tongxing.util.HttpUtil;
import com.kaiyiweitong.tongxing.util.SpUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2017/12/29 0029.
 */

public class AfterSellFragment extends Fragment {
    private static final String State = "state";
    private String mState;
    private View mView;
    private AfterSellActivity mActivity;
    private AfterSellItemAdapter mAdapter;
    private List<AfterSellResData> mData = new ArrayList<>();

    public static AfterSellFragment newInstance(String param1) {
        AfterSellFragment fragment = new AfterSellFragment();
        Bundle args = new Bundle();
        args.putString(State, param1);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            mState = getArguments().getString(State);
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        mActivity = (AfterSellActivity) getActivity();
        mView = inflater.inflate(R.layout.fragment_after_sell, container, false);
        init();
        initData();
        return mView;
    }

    private void init() {
        mView.findViewById(R.id.no_data).setVisibility(View.INVISIBLE);

        RecyclerView rv = mView.findViewById(R.id.rv_after_sell);
        rv.setLayoutManager(new GridLayoutManager(mActivity, 1, GridLayout.VERTICAL, false));
        mAdapter = new AfterSellItemAdapter(mActivity, mData);
        rv.setAdapter(mAdapter);
    }


    private void initData() {
        Map<String, String> map = new HashMap<>();
        map.put("id", SpUtils.getString(mActivity, LogicConstant.USER_ID));
        map.put("state", mState);
        HttpUtil.get(HttpConstant.USER_AFTER_SELL, map, mActivity, new HttpUtil.MyCallBack() {
            @Override
            public void onResult(String result) {
                if (result != null && !result.equals("")) {
                    List<AfterSellResData> data = HttpUtil.checkArrayResponse(mActivity, result, AfterSellResData.class);
                    if (data != null && data.size() > 0) {
                        mData.clear();
                        mData.addAll(data);
                        mAdapter.notifyDataSetChanged();
                    }
                }
            }
        });
    }
}
